/*******************************************************************
File Name:     apfastTiming.cpp
Author:        Shawn.Zhang
Security:      SEACHANGE SHANGHAI
Description:   implement class apHiResElapsedTime
Function Inventory: 
Modification Log:
When           Version        Who				What
---------------------------------------------------------------------
2007/06/07		1.0.0.0		   Shawn.Zhang		Create
********************************************************************/

#include "stdafx.h"

// We want this to be compatible with MFC and non-MFC apps
#ifdef _AFXDLL
#include <afx.h>
#else
#include <windows.h>
#endif

#include "apfastTiming.h"

/// apHiResElapsedTime

apHiResElapsedTime::apHiResElapsedTime () : starting_ (0)
{
	reset ();
}


double apHiResElapsedTime::usec () const
{
	return sec() * 1000000.;
}

double apHiResElapsedTime::msec () const
{
	return sec() * 1000;
}

double apHiResElapsedTime::sec () const
{
	LARGE_INTEGER t, freq;
	QueryPerformanceCounter (&t);
	QueryPerformanceFrequency (&freq);

	return (double(t.QuadPart - starting_)) / freq.QuadPart;
}


void apHiResElapsedTime::reset ()
{
	LARGE_INTEGER t;
	QueryPerformanceCounter (&t);

	starting_ = t.QuadPart;
}

////////////////////////

apResElapsedTime::apResElapsedTime () : starting_ (0)
{
	reset ();
}


double apResElapsedTime::usec () const
{
	return 0/*sec() * 1000000.*/;
}

double apResElapsedTime::msec () const
{
	SYSTEMTIME st;
	GetLocalTime(&st);
	int h, m, s;
	h = st.wHour;
	m = st.wMinute;
	s = st.wSecond;
	LONGLONG llNow = h * 3600 + m * 60 + s;
	llNow = llNow * 1000 + (int)st.wMilliseconds;
	
	return (double)(llNow - starting_);
}

double apResElapsedTime::sec () const
{
	return (msec() / 1000.0f);
}


void apResElapsedTime::reset ()
{
	SYSTEMTIME st;
	GetLocalTime(&st);
	int h, m, s;
	h = st.wHour;
	m = st.wMinute;
	s = st.wSecond;
	LONGLONG llNow = h * 3600 + m * 60 + s;
	llNow = llNow * 1000 + (int)st.wMilliseconds;

	starting_ = llNow;
}